package com.xmy.cultivate.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Quarter;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CommonUtil {

    /**
     * 判断两个值的季度相差多少
     */
    public static Integer diffQuarter(Map<String,Integer> map){
        Integer oldYear = map.get("oldYear");
        Integer oldNum = map.get("oldNum");
        Integer newYear = map.get("newYear");
        Integer newNum = map.get("newNum");

        Integer diffYear = newYear - oldYear;
        Integer diffNum = (newYear - oldYear) * 4 + newNum - oldNum;

        return diffNum;
    }

    public static int generate4DigitNumber() {
        Random random = new Random();
        int num = random.nextInt(9000) + 1000; // 生成1000到9999之间的随机数
        return num;
    }

    /**
     * 获取年级
     * @param id
     * @return
     */
    public static String getYearClassNameForId(Integer id){
        String[] dataArr = new String[]{"幼儿园","一年级","二年级","三年级","四年级","五年级","六年级","初一","初二","初三","高一","高二","高三"};
        String name = "";
        if(id>=0 && id<dataArr.length){
            name =dataArr[id];
        }
        return name;
    }

    /**
     * 获取年级段，如少儿,小学，初中,高中（0，1，2年级属于少儿）
     * @return
     */
    public static Integer getGradeSection(Integer yearClassId){
        Integer gradeSection = 100;//默认100
        if (yearClassId <=2){
            gradeSection = 0;
        }else if (yearClassId >2 && yearClassId<=6){
            gradeSection = 1;
        }else if (yearClassId>6 && yearClassId<=9){
            gradeSection = 2;
        }else if (yearClassId>9 && yearClassId<=12){
            gradeSection = 3;
        }
        return gradeSection;
    }

    public static String getYearClassIdsForGradeSection(Integer gradeSection){
        String[] dataArr = new String[]{"0,1,2","3,4,5,6","7,8,9","10,11,12"};
        String yearClassIds="";
        if(gradeSection>=0 && gradeSection<dataArr.length){
            yearClassIds =dataArr[gradeSection];
        }
        return yearClassIds;
    }


    /**
     * 获取下一学期
     * @param yearPart
     * @param quarterNum
     * @return
     */
    public static Quarter getNextQuarter(Integer yearPart, Integer quarterNum){
        Integer nextYearPart = yearPart;
        Integer nextQuarterNum = 0;
        if (quarterNum.equals(4)){
            nextQuarterNum =1;
            nextYearPart = nextYearPart + 1;
        }else {
            nextQuarterNum = quarterNum+1;
        }
        Quarter quarter = new Quarter();
        quarter.setYearPart(nextYearPart);
        quarter.setNum(nextQuarterNum);
        return quarter;
    }

    /**
     * 获取学期
     * @param id
     * @return
     */
    public static String getQuarterNameForId(Integer id){
        String[] dataArr = new String[]{"","寒假班","春季班","暑假班","秋季班"};
        String name = "";
        if(id>=0 && id<dataArr.length){
            name =dataArr[id];
        }
        return name;
    }

    /**
     * 获取上次期末
     * @param yearPart
     * @param quarterNum
     * @return
     */
    public static Quarter getUpQuarterForTerm(Integer yearPart,Integer quarterNum){
        Integer upYearPart = yearPart;
        Integer upQuarterNum = 0;
        Integer upExamNum = 3;//期末
        if (quarterNum.equals(2)){
            upQuarterNum =4;
            upYearPart = upYearPart - 1;
        }else if (quarterNum.equals(4)){
            upQuarterNum =2;
        }else if(quarterNum.equals(1)){
            upQuarterNum =4;
            upYearPart = upYearPart - 1;
        }else if(quarterNum.equals(3)){
            upQuarterNum =2;
        }
        String name = getQuarterNameForId(upQuarterNum);
        Quarter quarter = new Quarter();
        quarter.setAliasName(name);
        quarter.setYearPart(upYearPart);
        quarter.setNum(upQuarterNum);
        quarter.setSort(upExamNum);
        return quarter;
    }

    /**
     * 获取考试名称
     * @param id
     * @return
     */
    public static String getExamNameForId(Integer id){
        String[] dataArr = new String[]{"","期中","期中摸底","期末","期末摸底","市模拟"};
        String name = "";
        if(id>=0 && id<dataArr.length){
            name =dataArr[id];
        }
        return name;
    }

    public static String replaceGradeName(String gradeName,String studentName){
        //String text = "这是一个示例文本【第一组数据】和其他数据【第二组数据】结束。";
        Pattern pattern = Pattern.compile("【(.*?)】");
        Matcher matcher = pattern.matcher(gradeName);
        String keyword1 = "";
        while (matcher.find()) {
            //System.out.println(matcher.group(1));
            keyword1 = matcher.group(1);
        }
        if (StringUtils.isNotBlank(keyword1)){
            gradeName = gradeName.replace(keyword1,studentName);
        }
        return gradeName;
    }

    public static int getReceiptNo(){
        int [] randMath = new int[]{1,2,3,4,5,6,7,8,9,0};
        Integer randReceiptNo = getNotSimple(randMath,7);
        return randReceiptNo;
    }

    public static int getNotSimple(int[] param, int len) {
        Random rand = new Random();
        for (int i = param.length; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = param[index];
            param[index] = param[i - 1];
            param[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < len; i++) {
            result = result * 10 + param[i];
        }
        return result;
    }

    //获取两个逗号隔开数据的交集
    public static String getStrIntersection(String str1, String str2) {
        // 将字符串按逗号分割成数组
        String [] arr1 = str1.split (",");
        String [] arr2 = str2.split (",");
        // 将第一个数组元素存入 HashSet 中
        Set<String> set = new HashSet<>();
        for (String element : arr1) {
            if (StringUtils.isNotBlank(element)) {
                set.add(element.trim());
            }
        }
        // 找出交集元素
        List<String> intersection = new ArrayList<>();
        for (String element : arr2) {
            if (StringUtils.isBlank(element)) {
                continue;
            }
            String trimmedElement = element.trim();
            if (set.contains(trimmedElement)) {
                intersection.add(trimmedElement);
                set.remove(trimmedElement);
            }
        }
        // 将交集元素用逗号连接成字符串
        StringBuilder result = new StringBuilder ();
        for (int i = 0; i < intersection.size (); i++) {
            if (i > 0) {
                result.append (",");
            }
            result.append (intersection.get (i));
        }
        return result.toString();
    }

    /**
     * 获取两个逗号隔开数据的并
     * @param str1
     * @param str2
     * @return
     */
    public static String getStrUnion(String str1, String str2) {
        // 将输入的字符串按逗号分割成字符串数组
        String[] arr1 = str1.split(",");
        String[] arr2 = str2.split(",");

        // 使用 HashSet 存储元素，以去除重复项
        Set<String> set = new HashSet<>();

        // 将第一个数组的元素添加到集合中
        for (String element : arr1) {
            if (StringUtils.isNotBlank(element)) {
                set.add(element.trim());
            }
        }

        // 将第二个数组的元素添加到集合中
        for (String element : arr2) {
            if (StringUtils.isNotBlank(element)) {
                set.add(element.trim());
            }
        }

        // 将集合中的元素转换为逗号分隔的字符串
        List<String> list = new ArrayList<>(set);
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            result.append(list.get(i));
            if (i < list.size() - 1) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 通过年级段获取年级
     * @param gradeSection
     * @return
     */
    public static String getSectionYearClassIds(String gradeSection){
        StringBuilder yearClassIdSb = new StringBuilder();
        if (gradeSection != null && !gradeSection.equals("")) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                if (section.equals("0")) {
                    yearClassIdSb.append("1,2");
                } else if (section.equals("1")) {
                    yearClassIdSb.append("3,4,5,6");
                } else if (section.equals("2")) {
                    yearClassIdSb.append("7,8,9");
                } else if (section.equals("3")) {
                    yearClassIdSb.append("10,11,12");
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
        }
        return  yearClassIdSb.toString();
    }

    public static String listLongToStr(List<Long> list) {
        StringJoiner joiner = new StringJoiner(",");
        for (Long num : list) {
            joiner.add(num.toString());
        }
        return joiner.toString();
    }

    /**
     * 两个list合并，并去重
     * @param list1
     * @param list2
     * @return
     */
    public static List<String> getListUnion(List<String> list1, List<String> list2) {

        // 使用 Stream 合并、过滤 null 并去重
        List<String> mergedList = Stream.concat(list1.stream(), list2.stream())
                .filter(Objects::nonNull)  // 过滤 null
                .distinct()
                .collect(Collectors.toList());
        return mergedList;
    }


    /**
     * 获取年级段，如幼儿圆,小学，初中,高中(0属于少儿)
     * @return
     */
    public static Integer getGradeSectionForYearClassId(Integer yearClassId){
        Integer gradeSection = 100;//默认100
        if (yearClassId ==0){
            gradeSection = 0;
        }else if (yearClassId >0 && yearClassId<=6){
            gradeSection = 1;
        }else if (yearClassId>6 && yearClassId<=9){
            gradeSection = 2;
        }else if (yearClassId>9 && yearClassId<=12){
            gradeSection = 3;
        }
        return gradeSection;
    }
}
